package com.yra.service.impl;

import com.yra.domain.Product;
import com.yra.mapper.ProductMapper;
import com.yra.service.ProductService;
import com.yra.util.PageQueryUtil;
import com.yra.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: yrq
 * @Date: Create in 8:45 2019/12/4
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageResult productListPage(PageQueryUtil pageQueryUtil) {
        List<Product> products = productMapper.productListPage(pageQueryUtil);
        int totalProduct = productMapper.getTotalProduct(pageQueryUtil);
        PageResult pageResult = new PageResult(products, totalProduct, pageQueryUtil.getLimit(), pageQueryUtil.getPage());
        return pageResult;
    }

    @Override
    public Product productById(int id) {
        return productMapper.productById(id);
    }

    @Override
    public PageResult queryProductList(PageQueryUtil pageQueryUtil) {
        List<Product> products = productMapper.productListPage(pageQueryUtil);
        int total = productMapper.getTotalProduct(pageQueryUtil);
        PageResult pageResult = new PageResult(products,total,pageQueryUtil.getLimit(), pageQueryUtil.getPage());
        return pageResult;
    }

    @Override
    public void insertProduct(Product product) {
        productMapper.insertProduct(product);
    }

    @Override
    public void updateProduct(Product product) {
        productMapper.updateProduct(product);
    }
}
